﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThreeByte.ActiveDeck.Config;

namespace ThreeByte.ActiveDeck.AddIn.Controls
{
    /// <summary>
    /// Interaction logic for BackgroundChooser.xaml
    /// </summary>
    public partial class BackgroundChooser : UserControl
    {

        #region Dependency Properties

        //The PageConfig that is being manipulated
        public static readonly DependencyProperty PageProperty = DependencyProperty.Register("Page",
            typeof(PageConfig), typeof(BackgroundChooser));

        public PageConfig Page {
            get {
                return (PageConfig)GetValue(PageProperty);
            }
            set {
                SetValue(PageProperty, value);
            }
        }

        #endregion

        public BackgroundChooser() {
            InitializeComponent();
        }

        private void ChooseNewBackground_Click(object sender, RoutedEventArgs e) {
            System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
            fileDialog.CheckFileExists = true;
            System.Windows.Forms.DialogResult result = fileDialog.ShowDialog();
            if(result != System.Windows.Forms.DialogResult.OK) {
                return;
            }

            string filePath = fileDialog.FileName;
            string fileName = fileDialog.SafeFileName;

            GraphicResource oldGraphic = Page.ParentSlideConfig.Parent.Graphics.GetGraphicByName(fileName);
            if(oldGraphic != null) {
                Page.ParentSlideConfig.Parent.Graphics.Remove(oldGraphic);
            }
            GraphicResource newGraphic = new GraphicResource(fileName, new Uri(filePath), ImageType.JPEG);
            Page.ParentSlideConfig.Parent.Graphics.Add(newGraphic);
            Page.Background = newGraphic;
        }
    }
}
